package com.lcc.generate.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lixy on 2016/12/15.
 */
public class DateUtil {

    private final static String DF_COMPACT = "yyyyMMdd";
    private final static String DF = "yyyy-MM-dd";
    private final static String TF = "yyyy-MM-dd HH:mm:ss";
    private final static String MF = "yyyy-MM-dd HH:mm";
    private final static String MF_COMPACT = "yyyyMMddHHmm";

    public static void main(String[] args) {
        System.out.println(getDate_yyMMdd());
    }

    public static String getTodayCompact() {
        return new SimpleDateFormat(DF_COMPACT).format(new Date());
    }

    public static String getCompact_TF(Date date) {
        return new SimpleDateFormat(TF).format(date);
    }
    public static String getCompact_DF(Date date) {
        return new SimpleDateFormat(DF).format(date);
    }


    public static Date parseDate(String dateStr) {
        if (dateStr == null || "".equals(dateStr)) {
            return null;
        }
        try {
            if (dateStr.length() > 16) {
                return new SimpleDateFormat(TF).parse(dateStr);
            }else if (dateStr.length() > 10) {
                return new SimpleDateFormat(MF).parse(dateStr);
            } else {
                return new SimpleDateFormat(DF).parse(dateStr);
            }
        } catch (ParseException e) {

        }
        return null;
    }

    /**
     * 获取两个日期天数差
     * @param star
     * @param endDay
     * @return
     */
    public static long getNumOfDays(Date star,Date endDay){
        return Math.abs((star.getTime()-endDay.getTime())/24/60/60/1000);
    }

    /**
     * 获取日期字符串
     * @return
     */
    public static String getDate_yyMMdd(){
        Calendar calendar = Calendar.getInstance();
        String m = (calendar.get(Calendar.MONTH) + 1) + "";
        String d = calendar.get(Calendar.DAY_OF_MONTH) + "";
        return "" + calendar.get(Calendar.YEAR) + (m.length()==1?"0"+m:m) + (d.length()==1?"0"+d:d) ;
    }

}
